package com.ruoyi.ldzlsc.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.ldzlsc.domain.vo.ScTaskVo;
import com.ruoyi.ldzlsc.service.IScTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 质检查询控制器
 * 为质检模块提供工单、工序、任务信息查询接口
 *
 * @author ldzl
 */
@RestController
@RequestMapping("/qc-query")
public class QcQueryController extends BaseController {

    @Autowired
    private IScTaskService scTaskService;

    /**
     * 查询任务列表（供质检模块使用）
     * 支持按工单编号、工序编号、任务编号等条件查询
     *
     * @param workOrderCode 工单编号
     * @param workOrderName 工单名称
     * @param processCode 工序编码
     * @param taskName 工序名称
     * @param taskCode 任务编号
     * @param status 任务状态
     * @return 任务列表
     */
    @GetMapping("/task-list")
    public TableDataInfo getTaskListForQc(
            @RequestParam(required = false) String workOrderCode,
            @RequestParam(required = false) String workOrderName,
            @RequestParam(required = false) String processCode,
            @RequestParam(required = false) String taskName,
            @RequestParam(required = false) String taskCode,
            @RequestParam(required = false) String status) {

        ScTaskVo query = new ScTaskVo();

        // 设置工单查询条件
        if (workOrderCode != null && !workOrderCode.trim().isEmpty()) {
            query.setWorkOrderName(workOrderCode);
        } else if (workOrderName != null && !workOrderName.trim().isEmpty()) {
            query.setWorkOrderName(workOrderName);
        }

        // 设置工序查询条件
        if (processCode != null && !processCode.trim().isEmpty()) {
            query.setProcessCode(processCode);
        }

        // 设置任务查询条件
        if (taskName != null && !taskName.trim().isEmpty()) {
            query.setTaskName(taskName);
        }

        // 设置状态查询条件
        if (status != null && !status.trim().isEmpty()) {
            query.setStatus(status);
        }

        startPage();
        List<ScTaskVo> list = scTaskService.selectScTaskList(query);
        return getDataTable(list);
    }

    /**
     * 查询所有任务列表（不分页，供质检模块下拉选择使用）
     * 支持按工单编号、工序编号、任务编号等条件查询
     */
    @GetMapping("/all-tasks")
    public R<List<ScTaskVo>> getAllTasksForQc(
            @RequestParam(required = false) String workOrderCode,
            @RequestParam(required = false) String workOrderName,
            @RequestParam(required = false) String processCode,
            @RequestParam(required = false) String taskName,
            @RequestParam(required = false) String taskCode,
            @RequestParam(required = false) String status) {

        ScTaskVo query = new ScTaskVo();

        // 设置工单查询条件
        if (workOrderCode != null && !workOrderCode.trim().isEmpty()) {
            query.setWorkOrderName(workOrderCode);
        } else if (workOrderName != null && !workOrderName.trim().isEmpty()) {
            query.setWorkOrderName(workOrderName);
        }

        // 设置工序查询条件
        if (processCode != null && !processCode.trim().isEmpty()) {
            query.setProcessCode(processCode);
        }

        // 设置任务查询条件
        if (taskName != null && !taskName.trim().isEmpty()) {
            query.setTaskName(taskName);
        }

        // 设置状态查询条件
        if (status != null && !status.trim().isEmpty()) {
            query.setStatus(status);
        }

        List<ScTaskVo> list = scTaskService.selectScTaskList(query);
        return R.ok(list);
    }
}
